// Package _3__股票的最大利润 main
// @Title _3__股票的最大利润
// @Description
// @Author  == 琳琳也叫王木木 ==  2022-02-15 10:59
// @Create  2022-02-15 10:59
package main

import (
	"fmt"
	"math"
)

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func maxProfit(prices []int) int {
	if len(prices) == 0 {
		return 0
	}
	var ans int
	minVal := math.MaxInt32
	for i := 0; i < len(prices); i++ {
		if prices[i] < minVal {
			minVal = prices[i]
		} else if prices[i]-minVal > ans {
			ans = prices[i] - minVal
		}
	}
	return ans
}

func main() {
	fmt.Println(maxProfit([]int{7, 1, 5, 3, 6, 4}))
	fmt.Println(maxProfit([]int{7, 6, 4, 3, 1}))
}
